package com.netease.nim.demo.main.viewholder;

import android.text.TextUtils;

import com.netease.nim.demo.session.helper.MessageHelper;
import com.netease.nim.demo.session.model.extension.ActivityAttachment;
import com.netease.nim.demo.session.model.extension.GuessAttachment;
import com.netease.nimlib.sdk.msg.attachment.FileAttachment;
import com.netease.nimlib.sdk.msg.attachment.MsgAttachment;
import com.netease.nimlib.sdk.msg.attachment.NotificationAttachment;
import com.netease.nimlib.sdk.msg.constant.MsgTypeEnum;

public class CommonRecentViewHolder extends RecentViewHolder {

	@Override
	protected String getContent() {
		if (recent.getUnreadCount() > 0) {
			return foldShowMessage(descOfMsg());
		} else {
			return descOfMsg();
		}
	}

	/**
	 * 折叠消息显示未读数（最大999）和最近一条消息
	 */
	protected String foldShowMessage(String content) {
		StringBuilder sb = new StringBuilder();
//		String format = NimCache.getContext().getString(R.string.main_msg_unread_msg_count);
//		sb.append(String.format(format, ReminderSettings.foldMessageShowRule(recent.getUnreadCount())));
		sb.append(" ").append(content);
		return sb.toString();
	}

	protected String descOfMsg() {
        MsgAttachment attachment = recent.getAttachment();

        // SDK本身只记录原始数据，第三方APP可根据自己实际需求，在最近联系人列表上显示缩略消息
        // 以下为一些常见消息类型的示例。
        MsgTypeEnum type = recent.getMsgType();
        if (attachment instanceof FileAttachment) {
            String desc = "[文件]";
            String fileName = ((FileAttachment)attachment).getDisplayName();
            if (type == MsgTypeEnum.image) {
                desc = "[图片]";
            } else if (type == MsgTypeEnum.audio) {
                desc = "[语音消息]";
            } else if (type == MsgTypeEnum.video) {
                desc = "[视频]";
            }
            if (!TextUtils.isEmpty(fileName)) {
                desc = desc + ": " + fileName;
            }
            return desc;
        } else if (attachment instanceof NotificationAttachment) {
            return MessageHelper.getTeamNotificationText(recent.getContactId(),
                    recent.getFromAccount(),
                    (NotificationAttachment) recent.getAttachment());
        } else if (attachment instanceof GuessAttachment) {
            GuessAttachment guess = (GuessAttachment) attachment;
            return guess.getValue().getDesc();
        } else if(attachment instanceof ActivityAttachment) {
        	return "[活动]";
        }

        return recent.getContent();
    }
}
